package xFGE.xDisplay.xUI
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class xMsgBox extends Sprite
	{
		public static const MSGBOX_TYPE_NORMAL:int = 1;
		
		public static const MSGBOX_TYPE_OK:int = 2;
		
		private var _title:TextField;
		
		private var _msg:TextField;
		
		private var _okBtn:xButton;
		
		private var _cancelBtn:xButton;
		
		private var _dragBar:MovieClip;
		
		private var _backGround:MovieClip;
		
		private var _okFunc:Function = null;
		
		private var _cancelFunc:Function = null;
		
		private var _parent:DisplayObjectContainer = null;
		
		private var _defaultHeight:Number;
		
		private var _defaultWidth:Number;
		
		public function xMsgBox()
		{
			super();
			initView();
		}
		
		private function initView():void
		{
			_title = getChildByName("__title") as TextField;
			if(_title == null)throw(new Error("不存在'__title'"));
			_title.mouseEnabled = false;
			_title.wordWrap = false;
			_msg = getChildByName("__msg")as TextField;
			if(_msg == null)throw(new Error("不存在'__msg'"));
			_defaultHeight = _msg.height;
			_defaultWidth = _msg.width;
			_msg.mouseEnabled = false;
			_msg.autoSize = TextFieldAutoSize.CENTER;
			_msg.wordWrap = true;
			_okBtn = getChildByName("__okBtn")as xButton;
			if(_okBtn == null) throw(new Error("不存在'__okBtn'"));
			_okBtn.SetTitle("确定");
			_cancelBtn = getChildByName("__cancelBtn") as xButton;
			if(_cancelBtn == null) throw(new Error("不存在''__cancelBtn"));
			_cancelBtn.SetTitle("取消");
			_dragBar = getChildByName("__dragBar") as MovieClip; 
			if(_dragBar == null )  throw(new Error("不存在'__dragBar'"));
			_backGround = getChildByName("__backGround") as MovieClip;
			
			
			_okBtn.addEventListener(MouseEvent.CLICK,_onOk);
			_cancelBtn.addEventListener(MouseEvent.CLICK,_onCancel);
			_dragBar.addEventListener(MouseEvent.MOUSE_DOWN,_onDragDown);
			_dragBar.addEventListener(MouseEvent.MOUSE_UP,_onDragUp);
		}
		
		/**
		 * 弹出消息框
		 * @param msg
		 * @param okFunction
		 * @param cancelFunction
		 * 
		 */
		public function Popup(title:String,msg:String,parent:DisplayObjectContainer,type:int = MSGBOX_TYPE_NORMAL,okFunction:Function = null,cancelFunction:Function = null):void
		{
			_title.htmlText = title;
			_msg.htmlText = msg;
			_okFunc = okFunction;
			_cancelFunc = cancelFunction;
			_parent = parent;
			if(_parent.stage){
				this.x = (_parent.stage.width - this.width)/2;
				this.y = (_parent.stage.height - this.height)/2;
			}
			if(type == MSGBOX_TYPE_OK){
				removeChild(_cancelBtn);
				_okBtn.x = (_backGround.width - _okBtn.width)/2;
			}
			AutoSize();
			_parent.addChild(this);
		}
		
		/**
		 * 关闭消息框 
		 * 
		 */
		public function CloseBox():void
		{
			if(_parent) _parent.removeChild(this);
		}
		
		/**
		 * 自适应大小 
		 * 
		 */
		public function AutoSize():void
		{
			var addWP:Number = 0 ;
			var addHP:Number = 0;
			
			if(_msg.width > _defaultWidth){
				addWP = (_msg.width - _defaultWidth) / _defaultWidth;
			}
			if(_msg.height > _defaultHeight){
				addHP = (_msg.height - _defaultHeight) / _defaultHeight;
			}
			
			_okBtn.x += _okBtn.x * addWP;
			_okBtn.y += _okBtn.y * addHP;
			_cancelBtn.x+=_cancelBtn.x *addWP;
			_cancelBtn.y+= _cancelBtn.y*addHP;
			_dragBar.width += _dragBar.width*addWP;
			_dragBar.height += _dragBar.height*addHP;
			_backGround.width += _backGround.width*addWP;
			_backGround.height+= _backGround.height*addHP;
		}
		/**
		 * 确定 按钮 
		 * @param eve
		 * 
		 */
		private function _onOk(eve:MouseEvent):void
		{
			if(_okFunc != null)_okFunc();
			CloseBox();
			return;
		}
		
		/**
		 * 取消按钮 
		 * @param eve
		 * 
		 */
		private function _onCancel(eve:MouseEvent):void
		{
			if(_cancelFunc != null)_cancelFunc();
			CloseBox();
			return;
		}
		
		/**
		 * 拖动区域 按下 
		 * @param eve
		 * 
		 */
		private function _onDragDown(eve:MouseEvent):void
		{
			this.startDrag();
		}
		
		/**
		 * 拖动区域 放下 
		 * @param eve
		 * 
		 */
		private function _onDragUp(eve:MouseEvent):void
		{
			this.stopDrag();
		}
		
	}
}